package com.csun.dsms.controller;

import com.csun.dsms.comment.base.dto.ModifyStatusDto;
import com.csun.dsms.comment.base.enums.ErrorCodeEnum;
import com.csun.dsms.comment.base.exception.BusinessException;
import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.annotation.ValidateAnnotation;
import com.csun.dsms.comment.core.support.CsunMessageSource;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsMenu;
import com.csun.dsms.model.domain.DsmsRole;
import com.csun.dsms.model.domain.DsmsRoleUser;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.dto.menu.MenuQueryDto;
import com.csun.dsms.model.dto.role.CheckRoleCodeDto;
import com.csun.dsms.model.dto.role.RoleBindMenuDto;
import com.csun.dsms.model.enums.RoleStatusEnum;
import com.csun.dsms.model.vo.menu.BindAuthVo;
import com.csun.dsms.model.vo.role.RoleVo;
import com.csun.dsms.service.DsmsRoleService;
import com.csun.dsms.service.DsmsRoleUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-01 11:53
 * @description: 角色管理拦截器
 */
@RestController
@RequestMapping("/role")
public class DsmsRoleController {

    @Resource
    private DsmsRoleService dsmsRoleService;
    @Resource
    private DsmsRoleUserService dsmsRoleUserService;
    @Resource
    private CsunMessageSource csunMessageSource;

    /**
     * 查看角色信息.
     *
     * @param id the id
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/queryRoleInfoById/{id}")
    public Wrapper queryRoleInfo(@PathVariable Long id) {

        DsmsRole dsmsRole = dsmsRoleService.selectByKey(id);

        return WrapMapper.ok(dsmsRole);
    }

    /**
     * 验证角色编码是否存在.
     *
     * @param checkRoleCodeDto the check role code dto
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/checkRoleCode")
    public Wrapper checkRoleCode(@RequestBody CheckRoleCodeDto checkRoleCodeDto) {

        boolean stat = dsmsRoleService.checkRoleCode(checkRoleCodeDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), stat);
    }

    /**
     * 获取角色菜单列表
     *
     * @param menuQueryDto
     *
     * @return
     */
    @Authorization
    @PostMapping(value = "/queryRoleMenuListWithPage")
    public Wrapper queryRoleMenuListWithPage(@RequestBody MenuQueryDto menuQueryDto) {

        List<DsmsMenu> menuList = dsmsRoleService.queryRoleMenuListWithPage(menuQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), new PageInfo<>(menuList));
    }

    /**
     * 获取菜单树.
     *
     * @param roleId the role id
     *
     * @return the menu tree by role id
     */
    @Authorization
    @PostMapping(value = "/getMenuTreeByRoleId/{roleId}")
    public Wrapper getMenuTreeByRoleId(@PathVariable Long roleId) {

        BindAuthVo bindAuthVo = dsmsRoleService.getMenuTreeByRoleId(roleId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), bindAuthVo);
    }

    /**
     * 分页查询角色信息.
     *
     * @param dsmsRole the dsms role
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryUacRoleListWithPage(@RequestBody DsmsRole dsmsRole) {

        PageHelper.startPage(dsmsRole.getPageNum(), dsmsRole.getPageSize());
        dsmsRole.setOrderBy("role.created_time desc");

        List<RoleVo> roleVoList = dsmsRoleService.queryRoleListWithPage(dsmsRole);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), new PageInfo<>(roleVoList));
    }

    /**
     * 查询角色列表
     *
     * @return
     */
    @Authorization
    @PostMapping(value = "/queryList")
    public Wrapper queryUacRoleList() {

        List<DsmsRole> roleList = dsmsRoleService.selectAll();

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), roleList);
    }

    /**
     * 删除角色信息.
     *
     * @param id the id
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/deleteById/{id}")
    public Wrapper deleteUacRoleById(@PathVariable Long id) {

        Integer result = dsmsRoleService.deleteRoleById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 批量删除角色.
     *
     * @param deleteIdList the delete id list
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/batchDeleteByIdList")
    public Wrapper batchDeleteByIdList(@RequestBody List<Long> deleteIdList) {

        Integer result = dsmsRoleService.batchDeleteByIdList(deleteIdList);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 修改角色状态.
     *
     * @param modifyStatusDto the modify status dto
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/modifyRoleStatusById")
    public Wrapper modifyUacRoleStatusById(@CurrentUser DsmsUser login,
                                           @RequestBody ModifyStatusDto modifyStatusDto) {

        Long roleId = modifyStatusDto.getId();
        if (roleId == null) {
            throw new BusinessException(ErrorCodeEnum.GB10021002);
        }

        Long userId = login.getId();

        DsmsRoleUser ru = dsmsRoleUserService.getByUserIdAndRoleId(userId, roleId);

        if (ru != null && RoleStatusEnum.DISABLE.getType().equals(modifyStatusDto.getStatus())) {
            throw new BusinessException(ErrorCodeEnum.GB10021003);
        }

        DsmsRole dsmsRole = new DsmsRole();
        dsmsRole.setId(roleId);
        dsmsRole.setStatus(modifyStatusDto.getStatus());
        dsmsRole.setUpdateInfo(login);

        Integer result = dsmsRoleService.update(dsmsRole);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }


    /**
     * 保存用户.
     *
     * @param dsmsRole the dsms role
     *
     * @return the wrapper
     */
    @Authorization
    @ValidateAnnotation
    @PostMapping(value = "/save")
    public Wrapper save(@CurrentUser DsmsUser login,
                        @Valid @RequestBody DsmsRole dsmsRole, BindingResult br) {

        Integer result = dsmsRoleService.saveRole(dsmsRole, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 修改角色信息
     *
     * @param dsmsRole
     *
     * @return
     */
    @Authorization
    @PostMapping(value = "/edit")
    public Wrapper editUacRole(@CurrentUser DsmsUser login,
                               @RequestBody DsmsRole dsmsRole) {

        Integer result = dsmsRoleService.editRole(dsmsRole, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 角色分配权限.
     *
     * @param roleBindMenuDto the role bind menu dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/bindMenu")
    public Wrapper bindMenu(@RequestBody RoleBindMenuDto roleBindMenuDto) {

        dsmsRoleService.bindMenu(roleBindMenuDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null));
    }

}
